<?php

namespace App\Http\Controllers\Home;

use App\Http\Controllers\Core\BaseController;
use App\Models\Category;
use App\Models\CompanyProfile;
use App\Models\District;
use App\Models\Job;
use App\Models\Resume;
use App\Models\JobResumeAccess;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Session;

class JobController extends BaseController
{
    /**
     * Create a new controller instance.
     *
     * @return void
     */
    public function __construct()
    {
        //$this->middleware('auth');
    }

    /**
     * Show the application dashboard.
     *
     * @param Request $request
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $category = Category::category_cache();
        $categories = Category::category_caches();
        $district = District::district_cache();
        //TODO 转移到model
        $params = $request->all();
        $jobs = Job::searchResult($params, $category);
        $data = [
            'category' => $category,
            'categories' => $categories,
            'district' => $district,
            'jobs' => $jobs,
            'params' => $params
        ];
        return view('home.job.index', $data);
    }

    public function info($id = 0, Request $request)
    {
        $member = Session::get('member');//登陆用户信息
        $job = Job::getJobInfo($id);    //获取工作信息

        if(!isSet($job->company_id)){
            echo '<script>alert("工作信息不存在")</script>';

            exit;
        }
        $company = CompanyProfile::find($job->company_id);   //企业信息
        if (!$company) {
            echo '公司信息不存在';exit;
            //跳转到错误页面
        }

        //相似度推荐
        $similar = Job::where('id', '!=', $job->id)->where('category', $job->category)->limit(10)->orderByRaw("RAND()")->get();
        if (isSet($member->id)) {
            $resume = Resume::where('user_id', $member->id)->get()->first();
            $isMailing = JobResumeAccess::where(['job_id' => $job->id, 'user_id' => $member->id])->get();
            $isMailing = $isMailing->first() ? true : false;
        } else {
            $resume = [];
            $isMailing = false;
        }
        //判断是否已经投递过当前工作


        $data = [
            'resume' => $resume,
            'job' => $job,
            'company' => $company,
            'member' => $member,
            'similar' => $similar,
            'isMailing' => $isMailing,
        ];

        return view('home.job.item', $data);
    }

    /*
     * 申请职位
     * */
    /**
     * @param Request $request
     * @return false|string
     */
    public function apply(Request $request)
    {

        $id = $request->get('id');
        $member = Session::get('member');
        //获取用户信息
        if (!isSet($member->id)) {
            return json_encode(['data' => '', 'msg' => '请先登陆', 'status' => 0]);
        }
        $resume = Resume::where(['user_id' => $member->id])->get();
        if (!$resume = $resume->first()) {
            return json_encode(['data' => '', 'msg' => '请到个人中心完善您的简历', 'status' => -1]);
        }
        if (!$resume->nickname) {
            return json_encode(['data' => '', 'msg' => '请完善您的简历，填写基本信息', 'status' => -1]);
        }
        $job = Job::find($id);
        if (!$job) {
            return json_encode(['data' => '', 'msg' => '职位已经失效', 'status' => 0]);
        }

        //变更指纹信息
        $job->resume_number++;//添加数据
        $job->dispose_number++;//添加数据
        $job->save();
        $access = new JobResumeAccess();
        $access->resume_id = $resume->id;
        $access->job_id = $id;
        $access->user_id = $member->id;
        $access->company_id = $job->company_id;
        $access->create_time = time();
        if ($access->save()) {
            return json_encode(['data' => '', 'msg' => '添加成功', 'status' => 1]);
        }

    }
}